var shopList = "";

//判断字符串是否为空
function isEmpty(strVal) {
    if (strVal == '' || strVal == null || strVal == undefined) {
        return true;
    } else {
        return false;
    }
}

function checkInput(){
	if(!validate_shop() || !validate_type() 
			|| !validate_loanmoney() || !validate_loantime()
			|| !validate_mobile()){
		return false;
	}
	return true;
}

function validate_shop(){
	shopList = "";
	$("#shoplist input[type=checkbox]").each(function(){
	    if(this.checked){
	    	shopList += $(this).val()+",";
	    }
	});
	if(shopList.length > 0){
		$("#err-shop").html("");
		return true;
	}else{
		$("#err-shop").html("店铺不能为空，请勾选店铺");
		return false;
	}
}

function validate_type(){
	var value = $("#type").val();
    if(isEmpty(value) || value.length < 2){
    	$("#err-type").html("贷款用途填写有误，请正确填写");
        return false;
    }else{
    	$("#err-type").html("");
        return true;
    }
}

function validate_loanmoney(){
	var value = $("#loanmoney").val();
	var pattern = /^([0-9]*\.?[0-9]*)$/
	if(isEmpty(value) || !pattern.test(value)){
	    $("#err-loanmoney").html("期望贷款金额格式有误，请正确填写");
	    return false;
	}else{
	    $("#err-loanmoney").html("");
	    return true;
	}
}

function validate_loantime(){
	var value = $("#loantime").val();
	var pattern = /^([0-9]{4}-[0-9]{2}-[0-9]{2})$/
	if(isEmpty(value) || !pattern.test(value)){
	    $("#err-loantime").html("时间格式有误，请正确填写，如2016-05-01");
	    return false;
	}else{
	    $("#err-loantime").html("");
	    return true;
	}
}

function closeLoan(guid){
	var pdata = {"guid":guid};
	$.ajax({
		url: "../loan/close",
		data: JSON.stringify(pdata),
		contentType: "application/json",
		dataType: 'json',
		type: "POST",
		success: function (result) { 
			if(result.status == 200){
				window.location = location;
			}else{
				zeroModal.error(result.data);
			}
        }
	});
}

function deleteLoan(guid){
	var pdata = {"guid":guid};
	$.ajax({
		url: "../loan/delete",
		data: JSON.stringify(pdata),
		contentType: "application/json",
		dataType: 'json',
		type: "POST",
		success: function (result) { 
			if(result.status == 200){
				window.location = location;
			}else{
				zeroModal.error(result.data);
			}
        }
	});
}

function validate_mobile() {
    var value = $("#mobile").val();
    var pattern = /^(0?(13|14|15|18)[0-9]{9})$/;
    if(isEmpty(value) || !pattern.test(value)){
    	$("#err-mobile").html("手机号格式不正确，请正确填写");
    	return false;
    }else{
    	$("#err-mobile").html("");
        return true;
    }
}

function apply(){
	if(checkInput()){
		var type = $("#type").val()
		var loanmoney = $("#loanmoney").val();
		var loantime = $("#loantime").val();
		var mobile = $("#mobile").val();
		
		var pdata = {"shoplist":shopList,"type":type,"loanmoney":loanmoney,"loantime":loantime,"mobile":mobile};
		console.log(pdata);
		$.ajax({
			url: "../loan/apply",
			data: JSON.stringify(pdata),
			contentType: "application/json",
			dataType: 'json',
			type: "POST",
			success: function (result) { 
				console.log(result);
				if(result.status == 200){
				    zeroModal.success({
				    	content: '贷款申请提交成功啦~',
			            contentDetail: '系统会马上处理的，静候佳音~',
			            okFn: function() {
			            	window.location = location;
			            }
				    });
				}else{
					zeroModal.error({
				    	content: result.data,
			            contentDetail: '谢谢您的配合~'
				    });
				}
	        }
		});
	}
}
